'''
Created on 2009-7-17

@author: roy
'''

from xml.dom import minidom 

class xmlParser:
    '''
    XML parser
    '''


    def __init__(self, xml):
        '''
        Constructor
        '''
        self.xmldoc = minidom.parseString(xml)
        
        
    def getNodeList(self, path):
        names = path.split('/')
        return self.__parse(self.xmldoc, names, 1)
        
        
    def __parse(self, root, names, level):
        nodeList = root.childNodes
        
        if level == len(names):
            return nodeList
        
        for i in range(len(nodeList)):
            #str1 = nodeList[i].nodeName
            #str2 = names[level]
            if nodeList[i].nodeName == names[level]:
                return self.__parse(nodeList[i], names, level+1)
            
        return None
    
    
    def getNodeValue(self, path):
        try:
            return self.getNodeList(path)[0].wholeText
        except:
            return None
            
        
def test():
    
    str = \
    r'''<conf><account><rank>30</rank></account></conf>
    '''
    
    #xmldoc = minidom.parseString(str)
    #print xmldoc.childNodes.count(1)
    
    xmlp = xmlParser(str)
    #nodelist = xmlp.getNodeList('grammar')
    #print len(nodelist)
    #print nodelist[0].nodeName
    
    print xmlp.getNodeValue('/conf/account/rank')
    
    
    
if __name__ == '__main__':
    test()   
    
      